import axios from 'axios';
const state={
    roleListData:[],
    roleSelections:[]
}
const getters={
    selectedIds(state) {
        let roleKey = state.roleKey;
        let result = [];
        state.roleSelections.forEach(ele => {
            result.push(ele[roleKey]);
        });
        return result;
    }
}

const actions={
    setRoleFun({
        state,
        getters,
        dispatch
    },params={}){
        console.log(params)
        let bones = new Promise((resolve, reject) => {
            let selectedIds = getters.selectedIds.join(",");
            return axios.post(
                params.API || state.API.setRole, Object.assign({},  {[state.roleKey+'s']:selectedIds},params.data)
            ).then((res) => {
                if (res.data.result == "1") {
                    resolve(res.data);
                    dispatch("dataList");
                } else {
                    resolve(res.data);
                    console.error(res.data);
                }
            }).catch((err)=>{
                reject(err.data);
                console.error(err.data);
            });
        });
        return bones;
    },
    getRoleList({
        state,
        commit
    },params={}){
        console.log(params)
        let bones = new Promise((resolve, reject) => {
            return axios.post(
                params.API || state.API.searchRole, Object.assign({}, params.data)
            ).then((res) => {
                if (res.data.result == "1") {
                    commit("updateInfo", ["roleListData", res.data.data.list]);
                    resolve(res.data);
                } else {
                    resolve(res.data);
                    console.error(res.data);
                }
            }).catch((err)=>{
                reject(err.data);
                console.error(err.data);
            });
        });
        return bones;
    }
}
export default {
    state,
    actions,
    getters
};
